package com.example.demo.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.example.demo.annotation.MyRequestBody;
import com.example.demo.config.BaiduTranslateConfig;
import com.example.demo.entity.TranslateResult;
import com.example.demo.utils.baiduTranslate.HttpGet;
import com.example.demo.utils.baiduTranslate.TransApi;
import com.example.demo.vo.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author
 * @Description
 */
@RestController
@RequestMapping("translate")
public class TranslateController {
    private static final Logger log = LoggerFactory.getLogger(TranslateController.class);

    @Autowired
    private BaiduTranslateConfig translateConfig;

    @PostMapping("/baidu")
    public Result baiduTranslate(@MyRequestBody String value, @MyRequestBody String to){
        Result result = new Result();
        TransApi transApi = new TransApi(translateConfig.getAppId(), translateConfig.getKey());
//        String auto = transApi.getTransResult(value, "auto", to);
        Random random = new Random();
        int salt = random.nextInt(9000) + 1000;
        log.debug("随机数为{}", salt);

        // md5加密参数
        String sign = DigestUtils.md5Hex(translateConfig.getAppId() + value + salt + translateConfig.getKey());
        String param = translateConfig.getBaiduApi() + "?q=" + value + "&from=auto&to=" + to + "&appid=" +
                translateConfig.getAppId() + "&salt=" + salt + "&sign=" + sign;
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(param, String.class);
//
//        Map<String, String> map = new HashMap<>();
//        map.put("q", translation);
//        map.put("from", "auto");
//        map.put("to", "zh");
//        map.put("appid", translateConfig.getAppId());
//        map.put("salt", String.valueOf(salt));
//        HttpGet.get(translateConfig.getBaiduApi(), map);

        JSONObject jsonObject = JSON.parseObject(forObject);
        TranslateResult translateResult = new TranslateResult();
        if (jsonObject != null) {
            JSONArray transResult = jsonObject.getJSONArray("trans_result");
            translateResult.setFrom(jsonObject.getString("from"));
            translateResult.setTo(jsonObject.getString("to"));
            translateResult.setSrc(transResult.getJSONObject(0).getString("src"));
            translateResult.setDst(transResult.getJSONObject(0).getString("dst"));
            log.debug(transResult.toString());
        }
        result.setMessage("success");
        result.setObject(translateResult);
        return result;
    }
}
